package com.caiyouxi.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.boss.buser.mapper.SysUserProfileMapper;
import com.caiyouxi.boss.buser.model.SysUserProfileBean;
import com.caiyouxi.common.SystemInit;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.system.mapper.SettingsMapper;
import com.caiyouxi.system.model.SettingsBean;
import com.caiyouxi.system.service.SysSetingService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysSetingServiceImpl extends BaseService implements SysSetingService{

    private Logger logger = Logger.getLogger(SystemInit.class);

    @Autowired
    SettingsMapper settingsMapper;
    @Autowired
    SysUserProfileMapper userProfileMapper;
    @Autowired
    RedisService redisService;

    //通过key值获取对象信息
    @Override
    public SettingsBean get_setting_bean(String key) {
        SettingsBean set_bean = settingsMapper.select_setting(key);
        return set_bean;
    }

    //通过key值获取value值
    @Override
    public Object get_setting_value(String key) {
        System.out.println("从redis获取value值");
        String value = (String) redisService.get(key);
        if(value == null){
            System.out.println("从数据库获取value值");
            SettingsBean set_bean = settingsMapper.select_setting(key);
            value = set_bean.getValue();
            redisService.set(key,value,(long)-1);
        }
        return value;
    }

    @Override
    public List<SettingsBean> selectAll() {
        return settingsMapper.selectAll();
    }

    @Override
    public ResData select_is_aptcha(SysUserProfileBean bean) {
        SettingsBean settingsBean = settingsMapper.select_setting("sys.captcha.login");
        if (StringUtils.isEmpty(settingsBean)){
            logger.info("settingsBean为null");
             return  failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"settingsBean为null",null);
        }
        int value = Integer.parseInt(settingsBean.getValue());
        if(value < 0){ //总是不显示
            return success(false);
        } else if (value == 0){ //0表示总是显示
            return success(true);
        }else { //大于零的数表示登录失败几次后显示图形验证码
            if (!StringUtils.isEmpty(bean.getUsername())){
                SysUserProfileBean userbean = userProfileMapper.get_user_by_username(bean.getUsername());
                if( userbean != null){
                    if (!StringUtils.isEmpty(userbean.getLast_login_log())){
                        JSONObject json = JSONObject.parseObject(userbean.getLast_login_log());
                        String attempts = json.getString("attempts");
                        if(!StringUtils.isEmpty(attempts)){
                            int i = Integer.parseInt(attempts);
                            if(i > value){ //失败次数大于指定次数
                                return success(true);
                            }else {
                                return success(false);
                            }
                        }else {
                            return success(false);
                        }
                    }else {
                        return success(false);
                    }
                }else{
                    return success(false);
                }
            }else {
                return success(false);
            }

        }
    }

    //添加
    @Override
    public ResData add_setting(SettingsBean bean) {
        return success(settingsMapper.add_setting(bean));
    }
    //更新
    @Override
    public ResData update_setting(SettingsBean bean) {
        return success(settingsMapper.update_setting(bean));
    }
    //删除
    @Override
    public ResData delete_setting(SettingsBean bean) {
        return success(settingsMapper.delete_setting(bean));
    }
}
